import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/user/login.vue'
import home from '../views/home/home.vue'
import movie from '../views/movie/movie.vue'
import qa from '../views/qa/qa.vue'
import user from '../views/user/user.vue'
import notfound from '../views/notfound/notfound.vue'
import edit from '../views/user/edit.vue'
import student from '../views/user/student.vue'
import article from '../views/home/article.vue'
import article1 from '../views/home/article1.vue'
import search from '../views/home/search.vue'
import searchres from '../views/home/searchres.vue'

import { getToken } from '@/utils/token.js'
Vue.use(VueRouter)

const routes = [{
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path: '/edit',
        name: 'edit',
        component: edit
    },
    {
        path: '/home',
        name: 'home',
        component: home,
        // 配置路由元信息
        meta: { showTab: true }
    },
    {
        path: '/movie',
        name: 'movie',
        component: movie,
        meta: { showTab: true }
    },
    {
        path: '/qa',
        name: 'qa',
        component: qa,
        meta: { showTab: true }
    },
    {
        path: '/user',
        name: 'user',
        component: user,
        meta: { showTab: true, isLogin: true }
    },
    {
        path: '/student',
        name: 'student',
        component: student
    },
    {
        path: '/notfound',
        name: 'notfound',
        component: notfound
    },
    {
        path: '/article',
        name: 'myarticle',
        component: article
    },
    {
        path: '/article1',
        name: 'article1',
        component: article1
    },
    {
        path: '/search',
        name: 'search',
        component: search
    },
    {
        path: '/searchres',
        name: 'searchres',
        component: searchres
            // meta: {
            //     keepAlive: true
            // }
    },
    // 重定向
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '*', // 以上均不匹配
        redirect: '/notfound'
    }
]

const router = new VueRouter({
    routes
})
export default router
// 注册导航守卫 (全局前置守卫  跳转之前执行)
router.beforeEach((to, from, next) => {
    // 判断用户没有没有登录
    if (to.meta.isLogin && !getToken()) {
        // 没有登录，则跳转登录页
        next({ name: 'login' })
    } else {
        // 有登录，正常跳转
        next()
    }
})